<?php
// +----------------------------------------------------------------------
// | 系统定时任务 控制器
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2017 http://www.bjkunya.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xiaoyao <xiaoyao@ydycn.com>
// +----------------------------------------------------------------------

namespace Weixin\Controller;
use Think\Controller;
class TimetaskController extends Controller {

	public function index(){
		$ip = get_client_ip();
		logw("IP:{$ip}",'/task');
	
	}
	
	//定时更新商品
	public function updatagood(){
		//IP验证
		set_time_limit(0);
		//ignore_user_abort();//关掉浏览器，继续执行
		$this->checktoken();

		$times    = time();
		$end_time = date('Y-m-d H:i:s',$times + 86400 + getStr(1,3));
		$user     = M('user')->field(['id','chance'])->where(['auto_issue'=>1,'chance'=>['gt',0],'vip_type'=>['gt',0]])->select();
		foreach($user as $val){
			$chance= 0;
			$good  = '';
			$good  = M('goods')->where(['status'=>0,'end_time'=>['LT',date('Y-m-d H:i:00')],'user_id'=>$val['id']])->getField('id',true);

			if($good){
				$chance = count($good);
				if($val['chance'] >= $chance){
					if($chance > $val['chance']){
						$chance = $val['chance'];
					}
					M()->startTrans();
					try{
						M('goods')->where(['id'=>['in',$good]])->save(['end_time'=>$end_time,'type'=>2]);
						M('user')->save(['id'=>$val['id'],'chance'=>$val['chance']-$chance]);

						//记录任务
						M('tasks')->add(['uid'=>$val['id'],'runtime'=>$times,'detail'=>implode(',',$good),'chance'=>$chance]);
						
						M()->commit();
						$str = implode(',',$good);
						logw('商品ID：'.$str,'/task');
					}catch (\Exception $e) {
						M()->rollback();
						break;
					}
				}
				$chance = 0;
			}
			$good  = '';	

			
		}
		if($user){
			logw($user,'/task/logs');
		}else{
			logw('没有匹配的条目','/task/logs');
		}
		
	}

	//处理过期订单
	public function back_order(){
		set_time_limit(0);
		$this->checktoken();

		$time  = time()-7200;//两小时
		$where = ['status'=>'0','createtime'=>['lt',$time]];
		$order = M('order')->where($where)->getField('id',true);

		if($order){
			M('order')->where(['id'=>['in',$order]])->delete();
			logw('订单ID：'.$order,'/task');
		}
		logw('处理过期订单','/task');
		
	}

	//自定义更新商品
	public function setgood_time(){
		$this->checktoken();
		$time = time();
		$date = date('Y-m-d H:i:s');
		$end_time = date('Y-m-d H:i:s',$time + 86400 + getStr(1,3));

		/*$where= array('end_time'=>array('LT',$date),'user_id'=>array('LT',1949));
		$data = array('end_time'=>$end_time,'type'=>2);

		$save = M('goods')->where($where)->save($data);
		$sql  = M('goods')->_sql();*/

		$sql  = "update  `goods` as g INNER JOIN user as u on u.id=g.user_id set g.`end_time`='{$end_time}' ";
		$sql .= "WHERE  g.`user_id` < 1949 AND u.`vip_type` = 0 and g.`end_time` < '{$date}'";
		$save = M()->execute($sql);
		if($save){
			logw("更新成功:\r\n".$sql,'/task/good');
		}else{
			logw("没有更新:\r\n".$sql,'/task/good');
		}
	}

	//令牌验证
	public function checktoken(){
		$get_ip = get_client_ip();
		$selfip = "139.224.16.135";
		if($get_ip != $selfip){
			logw("IP:{$get_ip},非法请求者",'/task');
			die;
		}	
	}

	public function set_time(){
		//ignore_user_abort();//关掉浏览器，PHP脚本也可以继续执行.
		set_time_limit(3000);
		$interval=3;// 每隔5s运行

		/*死循环
		do{
			$time = time();
			p('测试'.$time); 
			sleep($interval);// 等待5s    
			p(round((time()-$time),4));
		}while(true);*/
		
		$time = time();
		for($i=0;$i<=100000;$i++){
			p('测试'.$time); 
			sleep($interval);// 等待
			p(round((time()-$time),4));
		}
		ob_flush();
		flush();

		/*定时执行
		require_once './curlClass.php';//引入文件

		$curl = new httpCurl();//实例化
		$stime = $curl->getmicrotime();
		for($i=0;$i<=10;$i++){

		echo '测试'.time().'<br/>'; 
		sleep($interval);// 等待5s

		}
		ob_flush();
		flush();
		$etime = $curl->getmicrotime();
		echo '<hr>';
		echo round(($etime-stime),4);//程序执行时间*/
	}

	
  //End
}
